const qs = require('../static/js/qs.js');
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
//封装接口
let baseURL = 'http://127.0.0.1:3000/';   //接口路径
let request = function (url, options = {}) {
  let myUrl = `${baseURL}${url}`;
  // 处理method,data,params=> 查询字符串
  // 如果有请求头
  // 合并对象data
  // 如果params 添加查询字符串参数
  //接口的参数
  if (options.params) {
    let urlQueryString = qs.stringify(options.params, {
      addQueryPrefix: true,
      allowDots: true,
    });
    myUrl += urlQueryString;
  }

  return new Promise((resolve, reject) => {
    wx.showLoading({
      title: '玩命加载中...',
    });
    wx.request({
      method: 'get',
      url: myUrl,
      success: resolve,
      fail: reject,
      complete: wx.hideLoading,
      ...options  //直接展开
    });
  });
}
module.exports = {
  formatTime: formatTime,
  request: request
}
